package dbcontrol

import (
	"MqttCloudProtocolServer/publiclib/datamodel/dborm"
)

//MqttserverControl struct
type MqttserverControl struct {
	base            dbOperabase
	Data            *dborm.Mqttserver
	ChildrenTCPConf []*ManageServerControl
}

//CreateMqttserverControl 初始化几种指针函数
func CreateMqttserverControl() *MqttserverControl {
	content := new(MqttserverControl)
	content.base.GetData = func() interface{} {
		return content.Data
	}
	content.base.SetData = func(data interface{}) {
		if data != nil {
			content.Data = data.(*dborm.Mqttserver)
		}
	}
	content.base.GetKey = func() interface{} {
		if content.Data == nil {
			return nil
		}
		return content.Data.ID
	}
	content.base.CreateNew = func() interface{} {
		res := new(dborm.Mqttserver)
		res.ID = 0
		return res
	}
	content.base.CreateNil = func() interface{} {
		return new(dborm.Mqttserver)
	}
	return content
}

//GetAll 获取全部用户-服务器映射信息
func (_this *MqttserverControl) GetAll() ([]*dborm.Mqttserver, error) {
	res := make([]*dborm.Mqttserver, 0)
	_, err := _this.base.GetAllByPage(&res, 0, 0, 0)
	return res, err
}

//CreateNew 创建一条记录
func (_this *MqttserverControl) CreateNew() {
	_this.Data = _this.base.CreateNew().(*dborm.Mqttserver)
}

//FindByID 根据主键获取一条记录
func (_this *MqttserverControl) FindByID(id int) error {
	return _this.base.FindByID(id)
}

//Save 更新当前条目
func (_this *MqttserverControl) Save() error {
	return _this.base.Save()
}

//Delete 删除当前条目
func (_this *MqttserverControl) Delete() error {
	return _this.base.Delete()
}

//GetChildrenTCPConf 获取用户-服务器映射
func (_this *MqttserverControl) GetChildrenTCPConf() error {
	tcpConfs := CreateTcpconfControl() // new(TcpconfControl)
	res, err := tcpConfs.GetByMqttServerID(_this.Data.ID)
	if err != nil || res == nil {
		return err
	}
	_this.ChildrenTCPConf = make([]*ManageServerControl, len(res))
	for i, item := range res {
		_this.ChildrenTCPConf[i] = CreateTcpconfControl() //new(TcpconfControl)
		_this.ChildrenTCPConf[i].Data = item
		_this.ChildrenTCPConf[i].ParentMqttServer = _this
	}
	return nil
}
